﻿using AutoMapper;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http.Results;
using System.Web.Mvc;
using WebInstagram.API;
using WebInstagram.DAL;
using WebInstagram.Models;
using WebInstagram.ViewModels;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;

namespace WebInstagram.Controllers
{
    public class HomeController : Controller
    {
        PhotosRep photoRep = new PhotosRep();

        ApplicationUserManager manager;

        public HomeController()
        {
            var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
            manager = new ApplicationUserManager(store);
        }

        [Authorize]
        public ActionResult Index(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
                userName = User.Identity.Name;
         
           var user = manager.FindByName(userName);
         
           if (user == null)
           {
               userName = User.Identity.Name;
           }
         
           ViewBag.UserName    = userName;
           ViewBag.LoggedUser = User.Identity.Name;

           var photos = photoRep.GetUserPhotos(userName);
           return View(photos);
        }

        [HttpGet]
        [Authorize]
        public ActionResult Recent()
        {
            PhotosRep rep = new PhotosRep();
            var photos = rep.Table.OrderByDescending(p => p.Created).AsEnumerable();

            return View(photos);
        }

        [HttpGet]
        [Authorize]
        public ActionResult UploadPhoto()
        {
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult UploadPhoto(string Title)
        {
            try
            {
                if (Request.Files[0] == null)
                {
                    ModelState.AddModelError("", "You've not choosed the picture to upload!");
                    return View(Title);
                }

                string name =  Request.Files[0].FileName;
                if (!name.Contains(".jpg") &&
                    !name.Contains(".png") &&
                    !name.Contains(".jpeg"))
                {
                    ModelState.AddModelError("", "File is not a picture!");
                    return View(Title);
                }

                MemoryStream target = new MemoryStream();
                Request.Files[0].InputStream.CopyTo(target);

                Photo newPhoto = new Photo();

                newPhoto.Image = target.ToArray();
                newPhoto.User = User.Identity.Name;
                newPhoto.Created = DateTime.Now;
                newPhoto.Title = Title;

                photoRep.Insert(newPhoto);
            }
            catch
            {
                ModelState.AddModelError("", "Error while saving the photo");
                return View(Title);
            }
            return RedirectToAction("Index");
            
        }
    }
}